package com.changgou.system.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.InetSocketAddress;

/**
 * @author kfzhao
 * @version 1.0
 * @className IpFilter
 * @description 获取客户端的访问ip
 * @date 2021/12/25 14:37
 */
//@Component
public class IpFilter implements GlobalFilter, Ordered {
    /**
     * 过滤器实现的具体逻辑
     *
     * @param exchange
     * @param chain
     * @return reactor.core.publisher.Mono<java.lang.Void>
     * @throws
     * @description
     * @author kfzhao
     * @date 2021/12/25 14:38
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 获取客户端的访问ip
        System.out.println("经过了第1个过滤器");
        ServerHttpRequest request = exchange.getRequest();
        InetSocketAddress remoteAddress = request.getRemoteAddress();
        System.out.println("ip: " + remoteAddress.getHostName());
        // 过滤器执行完毕，放行，向下执行业务
        return chain.filter(exchange);
    }


    /**
     * 过滤器的执行优先级：
     * 当前方法返回值越小，执行优先级越高
     *
     * @param
     * @return int
     * @throws
     * @description
     * @author kfzhao
     * @date 2021/12/25 14:39
     */
    @Override
    public int getOrder() {
        return 1;
    }

}
